package com.bosch.in.rtp1.ept.util.ehcache;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

/**
 * Wrapper original Ehcache method inside and implement simple interface put/get function
 * @author TPR1HC
 *
 * @param <K>
 * @param <V>
 */
public class EhcacheWrapper<K, V> implements CacheWrapper<K, V> 
{
    private final String cacheName;
    private final CacheManager cacheManager;

    public EhcacheWrapper(final String cacheName, final CacheManager cacheManager)
    {
        this.cacheName = cacheName;
        this.cacheManager = cacheManager;
    }

    public void put(final K key, final V value)
    {
        getCache().put(new Element(key, value));
    }

    @SuppressWarnings("unchecked")
	public V get(final K key) 
    {
        Element element = getCache().get(key);
        if (element != null) {
            return (V) element.getObjectValue();
        }
        return null;
    }

    public Ehcache getCache() 
    {
        return cacheManager.getEhcache(cacheName);
    }
    
    public void invalidate(final K key) {
    	getCache().remove(key);
    }
    
    public void invalidate() {
    	getCache().removeAll();
    }
}
